import Vue from 'vue'
import Vuex from 'vuex'
import service from "../utils/request";
import {getToken, removeToken} from "../utils/token";
import {resetRouter} from "../router";

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        // 左侧菜单栏数据
        menuItems: [],
        userInfo: {},
        roles: [],
        isAdmin: false,
        upHeaders: {},
        is401: false
    },
    mutations: {
        setMenus(state, items) {
            state.menuItems = [...items]
        },
        setUserInfo(state,userInfo){
            state.userInfo = userInfo
        },
        setUpHeaders(state,token){
            state.upHeaders = {
                'Authorization': 'Bearer ' + token
            }
        },
        setRoles(state,roles){
            state.roles = roles
        },
        setIsAdmin(state,isAdmin){
            state.isAdmin = isAdmin
        },
        setIs401(state,status){
            state.is401 = status
        }
    },
    actions: {
        // 获取菜单信息
        GetMenus({ commit, state }) {
            return new Promise((resolve, reject) => {
                service({
                    url: '/menu',
                    method: 'get'
                }).then(res => {
                    const menus = res.data
                    commit('setMenus', menus)
                    commit('setUpHeaders', getToken())
                    commit('setIs401',false)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                service({
                    url: '/getInfo',
                    method: 'get'
                }).then(res => {
                    let isAdmin = false
                    for (let key in res.roles) {
                        //判断权限
                        if (res.roles[key] === 'ROLE_SUPER_ADMIN' || res.roles[key] === 'ROLE_ADMIN'){
                            isAdmin = true
                            break
                        }
                    }
                    commit('setIsAdmin', isAdmin)
                    commit('setUserInfo', res.data)
                    commit('setRoles', res.roles)

                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        //更新用户信息
        updateInfo({commit, state}){
            service({
                url: '/update/adminInfo',
                method: 'post'
            }).then(res => {
                commit('setUserInfo', res.data)
            })
        },

        //更新用户信息
        setIs410Status({commit, state},status){
            commit('setIs401', status)
        },

        // 退出系统
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                service({
                    url: '/logout',
                    method: 'post'
                }).then(() => {
                    commit('setMenus', [])
                    commit('setUserInfo', {})
                    commit('setIsAdmin', false)
                    commit('setRoles', [])
                    removeToken()
                    // 重设路由
                    resetRouter()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('setMenus', [])
                commit('setUserInfo', {})
                commit('setIsAdmin', false)
                commit('setRoles', [])
                removeToken()
                // 重设路由
                resetRouter()
                resolve()
            })
        }
    }
})

export default store